<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <h2>电子商城管理系统</h2>
        </div>
      </template>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="0"
        size="large">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="User">
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="Lock"
            show-password>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="login-button"
            :loading="loading"
            @click="handleLogin">
            登录
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 添加注册链接 -->
      <div class="register-link">
        <el-link type="primary" @click="$router.push('/register')">
          还没有账号？去注册
        </el-link>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { loginAPI } from '../api/user'
import { setToken } from '../utils/token'
import { useUserStore } from '../store'

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const userStore = useUserStore()

const handleLogin = () => {
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try{
        const res = await loginAPI(loginForm)
        console.log(res);
        
        if(res.code!=='200'){
          ElMessage.error(res.msg? res.msg:'登录失败')
          loading.value = false
          return 
        }
        setToken('Bearer '+res.data.token)
        userStore.setUserInfo(res.data)
      }catch(err){
        ElMessage.error(err.msg? err.msg:'登录失败')
        loading.value = false
        return
      }finally{
        loading.value = false
      }
      // 登录成功后跳转到管理页面
      console.log('登录成功');
      ElMessage.success('登录成功')
      
      router.push('/admin')
    }
  })
}
</script>

<style scoped>
.login-container {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #2d3a4b;
}

.login-card {
  width: 400px;
}

.card-header {
  text-align: center;
}

.card-header h2 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.login-button {
  width: 100%;
}

.register-link {
  text-align: center;
  margin-top: 15px;
}
</style>